/**
 * <p>模态对话框控件</p>
 * xtype = cceafmessagebox

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.MessageBox
 * @extends Ext.util.Observable
 * @since 1.0
 */
Ext.define('cceaf.control.MessageBox',{
	extend: 'Ext.util.Observable', 
    alias: 'widget.cceafmessagebox',
    
             
    constructor: function(config){
		config = config || {};
		
		config = Ext.apply(config || {}, {

		});

		cceaf.control.MessageBox.superclass.constructor.apply(this, arguments);
	},

	title: '你好',
	message: '哈哈',
	height: 80,
	width: 250,
	pic:'/cceafweb/images/icon-info.gif',
	callback: null,
	
	showMessage : function(){
		var sf = this;     	
    	var windowTpl = new Ext.Template(sf.getBodyTpl());
		var shadowTpl = new Ext.Template(sf.getShadowTpl());
		var coverTpl = new Ext.Template(sf.getCoverTpl());
		sf.width = 1*(sf.width||250);
    	sf.height = 1*(sf.height||80);
		sf.cover = coverTpl.insertFirst(document.body, {}, true);
		sf.wrap = windowTpl.insertFirst(document.body, {title:sf.title,message:sf.message,width:sf.width,height:sf.height,pic:sf.pic}, true);
		sf.shadow = shadowTpl.insertFirst(document.body, {}, true);
        sf.shadow.setWidth(sf.wrap.getWidth());
        sf.shadow.setHeight(sf.wrap.getHeight());
        
		this.center();
		this.button = this.wrap.child('input[atype="window.button"]');
	},
	
	initEvents: function() {
		this.button.on('click',this.onClick);
		
    	cceaf.control.MessageBox.superclass.initEvents.call(this);
    },
	
    getBodyTpl : function(){
		return [
			'<TABLE class="win-table" cellSpacing="0" cellPadding="0" border="0" style="width:{width}px;">',
				'<TBODY>',
					'<TR>',
						'<TD class="win-table-head">{title}</TD>',
					'</TR>',
					'<TR class="win-table-body" style="height:{height}px;">',						
						'<TD><IMG src="{pic}" style="float:left; margin-left:45px;"/><SPAN style="float:left; margin-left:10px;">{message}</SPAN></TD>',
					'</TR>',
					'<TR class="win-table-footer">',
						'<TD align="center"><INPUT type="button" value="确定" atype="window.button"/></TD>',
					'</TR>',
				'</TBODY>',
			'</TABLE>'
		];
	},
	
	getShadowTpl : function(){
		return ['<DIV class="win-shadow"></DIV>'];
	},
	
	getCoverTpl : function(){
		return ['<DIV class="dreamer-cover"></DIV>'];
	},
	
	center: function(){
    	var screenWidth = screen.width;
    	var screenHeight = screen.height;
    	var sl = document[Ext.isStrict&&!Ext.isWebKit?'documentElement':'body'].scrollLeft;
    	var st = document[Ext.isStrict&&!Ext.isWebKit?'documentElement':'body'].scrollTop;
    	var x = sl+Math.max((screenWidth - this.width)/2,0);
    	var y = st+Math.max((screenHeight - this.height-(Ext.isIE?26:23))/2,0);        
        this.move(x,y-100);      
    },
    
	/**
     * 移动窗口到指定位置.
     * 
     */
    move: function(x,y,m){
        this.wrap.moveTo(x,y);
        if(!m)this.shadow.moveTo(x+3,y+3)
    },
    
	onClick : function(){
		if(this.callback && this.callback.call(this) === false) {
			return;
		}
		
		this.close();
	},
	
	close : function(){
		Ext.fly(this.cover).remove();
		Ext.fly(this.wrap).remove();
		Ext.fly(this.shadow).remove();
	}
	
});

var ExtUxCceafControlMessageBox = cceaf.control.MessageBox = new cceaf.control.MessageBox();

